package model

import (
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	UserID        uint   `gorm:"type:int;comment:下单用户id"`
	No            string `gorm:"type:varchar(50);comment:订单编号;unique"`
	Name          string `gorm:"type:varchar(20);comment:收货人姓名"`
	Phone         string `gorm:"type:char(11);comment:收货人手机号"`
	Address       string `gorm:"type:varchar(255);comment:地址"`
	Note          string `gorm:"type:varchar(255);comment:订单备注"`
	CouponID      uint   `gorm:"type:int;comment:优惠券id"`
	Favorable     string `gorm:"type:decimal(10,2);comment:优惠价格"`
	Amount        string `gorm:"type:varchar(30);comment:订单总金额"`
	PayType       uint   `gorm:"type:tinyint(1);comment:支付方式（0支付宝 1微信）"`
	CommentStatus bool   `gorm:"type:tinyint(1);comment:评论状态（0待审核1审核完成2审核失败）"`
	PaymentStatus uint   `gorm:"type:tinyint(1);comment:支付状态（0待支付1支付成功2支付失败）"`
	Status        uint   `gorm:"type:tinyint(1);comment:订单状态（0未支付1待发货）"`
}

func (m *Order) CreateOrderWithTx(tx *gorm.DB, orderGoods []*OrderGoods) error {
	err := tx.Create(m).Error
	if err != nil {
		return err
	}
	for key := range orderGoods {
		orderGoods[key].OrderID = m.ID
	}

	return tx.Create(orderGoods).Error
}
